<template>
  <div>
    <div class="table-operator" v-if="stage !== '05010' && stage !== '05050'">
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <!--            <a-col :md="6" :sm="8">-->
          <!--              <a-form-item label="取证编码">-->
          <!--                <a-input v-model="queryParam.number"></a-input>-->
          <!--              </a-form-item>-->
          <!--            </a-col>-->
          <a-button type="primary" @click="searchQuery" icon="redo">刷新</a-button>
          <!--          <a-button type="primary" style='background-color: #FFA500;border:0' @click="OnExportList">
                      导出
                    </a-button>-->

        </a-form>
      </div>
    </div>
    <div v-if="stage !== '05010' && stage !== '05050'" class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
      <i class="anticon anticon-info-circle ant-alert-icon"></i>
      已选择&nbsp;
      <template v-if="selectTypes && selectTypes.length > 0">
        <a style="font-weight: 600" v-for="(item,index) in selectTypes" :key="item.state">
          {{ item.state_dictText }} {{ item.count }}项&nbsp;&nbsp;</a>
      </template>
      <a style="font-weight: 600" v-else>0项&nbsp;&nbsp;</a>

      <a style="margin-left: 24px" @click="onClearSelected">清空多选</a>

      <a-space>
        <a-divider type="vertical" />
        <a-button :disabled="approveButType != '1'" size="small" type="primary" @click="showConfirm">
          提交复核
        </a-button>
        <a-button :disabled="approveButType != '2'" size="small" @click="setApproveReason('9')">
          同意
        </a-button>
        <a-button :disabled="approveButType != '2'" size="small" type="danger"
                  @click="setApproveReason('1')">
          拒绝
        </a-button>
      </a-space>
    </div>
    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :rowSelection="rowSelection"
      @change="handleTableChange"
    >
      <span slot="matter" slot-scope="text">
        <j-ellipsis :value="text" :length="15" />
      </span>
      <span slot="accord" slot-scope="text">
        <j-ellipsis :value="text" :length="15" />
      </span>
      <span slot="proName" slot-scope="text">
        <j-ellipsis :value="proName" :length="15" />
      </span>
      <span slot="actionName">
        <a-button v-if="stage !== '05010' && stage !== '05050'" size="small" @click="handleAdd('新增取证')"
                  type="primary" icon="plus">新增</a-button>
        <span v-else>操作</span>
      </span>
      <span
        v-if="stage !== '05010'
        && stage !== '05050'
        && ((record.state == '0' && record.auditUserId == userInfo.id) || (record.state == '1' && record.auditUserId == userInfo.id) || (record.state == '2' && record.reviewer == userInfo.id) || (record.state == '4' && record.auditUserId == userInfo.id))"
        slot="action"
        slot-scope="text, record">
          <a @click="handleEdit(record,'编辑取证')">编辑</a>
          <a-divider type="vertical" />
          <a @click="exportQz(record.id)">导出</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleCheck(record,'查看')">查看</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
      </span>
      <span v-else slot-scope="text, record">
        <template v-if="stage !== '05010'
        && stage !== '05050'
        && (record.state == '3' && record.auditUserId == userInfo.id)">
          <a @click="handleEdit(record,'编辑取证')">编辑</a>
          <a-divider type="vertical" />
        </template>
        <a @click="handleCheck(record,'查看')" style="color: #52C41A">查看</a>
        <a-divider type="vertical" />
        <a @click="exportQz(record.id)">导出</a>
      </span>
    </a-table>

    <QzModel ref="modalForm" :id="id" :bsjdwName="bsjdwName" :bsjdw="bsjdw" :protype="protype" :type="type"
             @ok="modalFormOk"></QzModel>
    <SelectQzApprover ref="approver" :id="queryParam.proId" @submitData="handleSubmit"></SelectQzApprover>
    <SetReason ref="setReason" @approveReason="approveData"></SetReason>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import columns from './indexColumns'
import CheckAndEditModel from '../sjwt/CheckAndEditModel'
import QzModel from '@views/sj/sjxmzygl/commView/sssj/sjyd/QzModel.vue'
import { getAction } from '@api/manage'
import { butStatus } from '@views/sj/sjxmzygl/butStatus'
import SetReason from '@views/sj/sjxmzygl/commView/public/SetReason.vue'
import SelectQzApprover from '@views/sj/sjxmzygl/commView/sssj/sjqz/SelectQzApprover.vue'
import store from '@/store'
import viewTypeId from '@views/sj/sjxmzygl/commView/public/typeId'

export default {
  props: ['id', 'bsjdwName', 'bsjdw', 'protype', 'type', 'stage', 'proName'],
  name: 'sjqz',
  components: {
    SelectQzApprover, SetReason,
    QzModel,
    CheckAndEditModel
  },
  mixins: [JeecgListMixin, butStatus],
  watch: {
    'queryParam.sstype': {
      handler: function(val) {
        this.loadData()
      },
      immediate: true,
      deep: true
    }
  },
  data() {
    return {
      //表头
      queryParam: {
        proId: this.id
      },
      typeId: viewTypeId['审计取证'],
      columns: columns(this),
      userInfo: store.getters.userInfo,
      url: {
        list: '/sjxmzygl/sjWorkZtfaEvidence/list',
        delete: '/sjxmzygl/sjWorkZtfaEvidence/remove',
        exportList: `/sjxmzygl/sjWorkZtfaEvidence/exportEvidenceInfo`,
        batchSubmit: '/sjxmzygl/sjWorkUsers/batchSubmitAuditEvidence',
        batchApprove: '/sjxmzygl/sjWorkUsers/batchZyEvidenceCommit'
      }
    }
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: (selectedRowKeys, selectedRows) => {
          this.selectedRowKeys = selectedRowKeys
          this.selectionRows = selectedRows
        },
        getCheckboxProps: record => ({
          props: {
            disabled: !((record.state == '2' && record.reviewer == this.userInfo.id) || ((record.state == '1' || record.state == '4') && record.auditUserId == this.userInfo.id))
          }
        })
      }
    },

    // 表格选中项分类项
    selectTypes() {
      return this.selectionRows.length > 0 ? this.selectionRows.reduce((acc, curr) => {
        if (!Array.isArray(acc)) {
          acc = []
        }
        const existing = acc.find(item => item.state == curr.state)
        if (existing) {
          existing.count++
        } else {
          acc.push({
            state: curr.state,
            state_dictText: curr.state_dictText,
            count: 1
          })
        }
        return acc
      }, {}) : []
    },

    approveButType() {
      if (this.selectionRows.length > 0) {
        if (this.selectionRows.every(item => item.state == '1' || item.state == '3' || item.state == '4')) {
          return '1'
        } else if (this.selectionRows.every(item => item.state == '2')) {
          return '2'
        }
      }
      return '0'
    }
  },
  methods: {
    handleAdd: function(name) {
      this.$refs.modalForm.add(this.queryParam)
      this.$refs.modalForm.title = name
      this.$refs.modalForm.isCheck = false
      this.$refs.modalForm.disableSubmit = false
    },
    exportQz(id) {
      getAction(this.url.exportList, { id: id }).then((res) => {
        if (res.success && res.result) {
          this.$message.success('正在下载，请稍等！')
          const url = `${window._CONFIG['domianURL']}/sys/common/static${res.result}`
          window.open(url, '_blank')
        } else {
        }
      })
    },

    // 批量提交审核
    handleSubmit(userId) {
      getAction(this.url.batchSubmit, {
        proId: this.queryParam.proId,
        userId: userId,
        typeId: this.typeId,
        evidenceIds: this.selectedRowKeys.toString()
      }).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.modalFormOk()
        } else {
          this.$message.error(res.message)
        }
      })
    },

    // 批量同意拒绝
    approveData(reason) {
      let remark = this.approveStatus === '9' ? `同意：${reason}` : `拒绝：${reason}`
      getAction(this.url.batchApprove, {
        proId: this.queryParam.proId,
        status: this.approveStatus,
        remark: remark,
        typeId: this.typeId,
        evidenceIds: this.selectedRowKeys.toString()
      }).then((res) => {
        if (res.success) {
          this.$refs.setReason.setApproveReason = false
          this.modalFormOk()
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style scoped>

</style>